package com.hp.ssm.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.alibaba.fastjson.JSON;
import com.hp.ssm.entity.User;
import com.hp.ssm.service.LoginService;
import com.hp.ssm.util.ResultJson;
import com.hp.ssm.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * @author 23129
 * 登录和权限限制
 */
@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    public void login(User user,String checkCode, HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        Map map = loginService.login(user);
        String code = (String) req.getSession().getAttribute("code");
        if (map != null && code.equals(checkCode)) {
            req.getSession().setAttribute("map", map);
            List<Map> permissionsList = loginService.getPermissions(Integer.valueOf(map.get("aid").toString()));
            System.out.println(JSON.toJSONString(permissionsList));
            req.getSession().setAttribute("permissionsList", permissionsList);
            ResultJson success = ResultJson.success(map);
            Utils.returnJson(success, resp);
        } else {
            ResultJson error = ResultJson.error();
            Utils.returnJson(error, resp);
        }
    }

    @RequestMapping("/getPermissions")
    public ModelAndView getPermissions(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        ModelAndView mv = new ModelAndView();
        Map map = (Map) req.getSession().getAttribute("map");
        req.getSession().setAttribute("map", map);
        List<Map> list = (List<Map>) req.getSession().getAttribute("permissionsList");
        ResultJson success = ResultJson.success(list);
        Utils.returnJson(success, resp);
        return mv;
    }
    /*
    * 校验表单内容*/
    @RequestMapping("/checkUser")
    public void checkUser(String ausername,HttpServletResponse resp){
        User user1 = loginService.checkUser(ausername);
        if(user1 != null) {
            ResultJson success = ResultJson.success(user1);
            Utils.returnJson(success, resp);
        }else {
            ResultJson error = ResultJson.error();
            Utils.returnJson(error,resp);
        }
    }
    /**
     * 获取验证码
     */
    @RequestMapping("/checkCode")
    public void checkCode(HttpSession session, HttpServletResponse response) throws IOException {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
        String code = circleCaptcha.getCode();
        session.setAttribute("code", code);
        circleCaptcha.write(response.getOutputStream());
    }
}
